﻿use [master]
go

declare @isreinstall bit = 'true';
if (exists(select 'true' from sys.databases where [name]=N'PoseidonAdminDB') and @isreinstall = 'true')
begin
drop database [PoseidonAdminDB]
end
go

DECLARE @DefaultData VARCHAR(100) 
EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE', 
@key='Software\Microsoft\Microsoft SQL Server\MSSQL10.SQLEXPRESS\Setup', 
@value_name='SQLDataRoot', 
@DefaultData=@DefaultData OUTPUT 

set @DefaultData =@DefaultData+'\data\'

if (not exists(select 'true' from sys.databases where [name]=N'PoseidonAdminDB'))
begin
declare @createDBSql varchar(8000) ='
CREATE DATABASE [PoseidonAdminDB] ON  PRIMARY 
( NAME = ''PoseidonAdminDB'', FILENAME = '''+@DefaultData+'PoseidonAdminDB.mdf'' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = ''PoseidonAdminDB_log'', FILENAME = '''+@DefaultData+'PoseidonAdminDB_log.ldf'' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
'
exec (@createDBSql)
end
go

CREATE DATABASE [PoseidonDB_{1}] ON  PRIMARY 
( NAME = 'PoseidonDB_1', FILENAME = '{0}PoseidonDB_{1}.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = 'PoseidonDB_1_log', FILENAME = '{0}PoseidonDB_{1}_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)


use [PoseidonAdminDB] 
go

if not exists(select 'true' from sys.tables where object_id=OBJECT_ID('KeyFile'))
begin
create table [KeyFile]
(
KeyId varchar(160) primary key not null ,
Content varchar(max) null,
IsInUse bit not null,
[Type] int not null,
DBPre varchar(100) null,
[IsDeleted] bit not null default('false'),
)
end

if not exists(select 'true' from sys.tables where object_id=OBJECT_ID('PoseidonAdmin'))
begin
create table [PoseidonAdmin]
(
AdminName nvarchar(100) primary key not null ,
Pwd nvarchar(100) not null,
CompanyName nvarchar(100) null,
RetrieveQuestion varchar(100) null,
RetrieveAnswer varchar(100) null,
Email varchar(100) null,
DBPre varchar(100) not null,
KeyId varchar(160) null ,
[IsDeleted] bit not null default('false'),
)
end

go
